// Register page module
// ====================

define(['zepto', 'sui', 'smExtend','submitForm' ], function($) {
  return {
    init: function(data) {
      this.initPage();
      this.bindSelectEvent();
    },
    initPage: function() {
      var that = this;
      $("form").submitform({
        beforeSubmit: function(el) {
          var parameters = $("form").serializeArray();
          for(var i=0; i<parameters.length; i++) {
            if(!parameters[i].value) {
              $.toast("请回答完所有问题");
              return false;
            }
          }
          return true;
        },
        successUrl: __CTX__.root + "/questionnaires/result",
      });
    },
    
    bindSelectEvent: function() {
      var that = this;
      var questionSwiper = $(".questionnaire-check").swiper({allowSwipeToNext : false});
      var swiperLength = $(".swiper-slide").length;
      $(document).on("click", ".answer-item", function(e) {
        e.preventDefault();
        $(this).siblings().removeClass("active")
        $(this).addClass("active")
        var currentIndex = questionSwiper.activeIndex;
        var option = $(this).data("selector") + "|" + $(this).find("div").text() + "|" + $(this).data("grades")
        $(this).parent(".answer-layout").find("input").val(option)
        $(".progress-item").eq(currentIndex).addClass("active")
        questionSwiper.slideTo(currentIndex+1, 1000, false);
        if((currentIndex+1) == swiperLength) {
          $(".questionnaire-submit").removeClass("hide");
        }
      })
      
      that.bindPrevNextClick(questionSwiper);
      
    },
    bindPrevNextClick: function(questionSwiper) {
    
      $(document).on("click", "span.prev", function() {
        var currentIndex = questionSwiper.activeIndex;
        questionSwiper.slideTo(currentIndex - 1, 1000, false);
      })
      
      $(document).on("click", "span.next", function() {
        
        var currentIndex = questionSwiper.activeIndex;
        var temp = 0;
        $(".swiper-slide-active .answer-item").each(function(index) {
           $(this).hasClass("active") ? temp = 1 : '';
        })
        temp == 1 ? questionSwiper.slideTo(currentIndex + 1, 1000, false) : $.toast("请选择一个选项",500)
        
      })
    }
  }
})